#include "strings_encoding.h"

std::wstring atou(const char *src, UINT encode) {
  std::wstring str;
  int len = MultiByteToWideChar(encode, 0, src, (int)strlen(src), NULL, 0);
  wchar_t *wcharTmp = new wchar_t[(size_t)len + 1];
  MultiByteToWideChar(encode, 0, src, (int)strlen(src), wcharTmp, len);
  wcharTmp[len] = '\0';
  str = wcharTmp;
  delete[] wcharTmp;
  return str;
}

std::wstring atou(const std::string &src, UINT encode) {
  return atou(src.c_str());
}

std::string utoa(const wchar_t *src, UINT encode) {
  std::string str;
  int len = WideCharToMultiByte(encode, 0, src, (int)wcslen(src), NULL, 0, NULL,
                                NULL);
  char *tmp = new char[(size_t)len + 1];
  WideCharToMultiByte(encode, 0, src, (int)wcslen(src), tmp, len, NULL, NULL);
  tmp[len] = '\0';
  str = tmp;
  delete[] tmp;
  return str;
}

std::string utoa(const std::wstring &src, UINT encode) {
  return utoa(src.c_str());
}

std::wstring atouu8(const char *src) { return atou(src, CP_UTF8); }

std::wstring atouu8(const std::string &src) { return atouu8(src.c_str()); }

std::string utoau8(const wchar_t *src) { return utoa(src, CP_UTF8); }

std::string utoau8(const std::wstring &src) { return utoau8(src.c_str()); }

std::string u8toa(const char *src) {
  auto tmp = atouu8(src);
  return utoa(tmp.c_str() /*, CP_OEMCP*/);
}

std::string u8toa(const std::string &src) { return u8toa(src.c_str()); }

std::string atou8(const char *src) {
  auto tmp = atou(src);
  return utoau8(tmp);
}

std::string atou8(const std::string &src) { return atou8(src.c_str()); }
